package com.rebate.web.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rebate.model.Announcement;
import com.rebate.model.Merchant;
import com.rebate.service.IBaseService;
import com.rebate.util.Page;
import com.rebate.util.PageUtils;

@Controller
public class AnnouncementServlet extends BaseServlet{
	
	private String view = "/announcement/listannouncement";
	private String editView = "/announcement/editannouncement";
	private String createView = "/announcement/createannouncement";
	@Autowired
	private IBaseService<Announcement> announcementService;
	
	/**
	 * 公告列表页面
	 * @throws IOException 
	 */
	@RequestMapping(value = "/announcement/index.htm")
	public String announcementIndex(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("announcement") Announcement announcement) throws IOException {
		return view;
	}
	
	/**
	 * 获得公告List
	 * @throws IOException 
	 */
	@RequestMapping(value = "/announcement/list.htm")
	public void announcementList(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("announcement") Announcement announcement) throws IOException {
		Page<Merchant> page = PageUtils.getPage(request);
		page = announcementService.queryForListPage(page, announcement);
		print(request, response, page);
	}
	
	/**
	 * init create page
	 * @throws IOException 
	 */
	@RequestMapping(value = "/announcement/add.htm")
	public void createInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("announcement") Announcement announcement) throws IOException {
		announcement.setCreateTime(new Date());
		Long id = announcementService.create(announcement);
		if (id != null) {
			response.getWriter().print(1);//创建成功
		}else {
			response.getWriter().print(0);
		}
	}
	
	
	/**
	 * edit announcement page
	 * @throws IOException 
	 */
	@RequestMapping(value = "/announcement/edit.htm")
	public String editInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("announcement") Announcement announcement) throws IOException {
		announcement = announcementService.findById(announcement.getId());
		model.put("announcement", announcement);
		return editView;
	}
	
	/**
	 * update announcement
	 * @throws IOException 
	 */
	@RequestMapping(value = "/announcement/update.htm")
	public void update(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("announcement") Announcement announcement) throws IOException {
		if (announcementService.update(announcement)) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	
}
